<?php
include "header.php";

$ac = isset($_POST["ac"]) ? trim($_POST["ac"]) : "";

$cat_handler = xoops_getmodulehandler("category");

switch($ac) {
    case "delete":
        if ( !$GLOBALS['xoopsSecurity']->check() ) {
            redirect_header("category.php", 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
        }
        $cat_id = isset($_POST["cat_id"]) ? intval($_POST["cat_id"]) : 0;
        
        $cat_obj = $cat_handler->get($cat_id);
        
        if ( empty($cat_obj) || !is_object($cat_obj) || $cat_obj->isNew() ) {
            redirect_header("category.php");
        }
        
        if ( $cat_handler->delCat($cat_id) ) {
            $stop = _GROUPS_AM_SUCCEED;
        } else {
            $stop = _GROUPS_AM_FAILED;
        }
        
        redirect_header("category.php",3,$stop);
        break;
    case "save":
        if ( !$GLOBALS['xoopsSecurity']->check() ) {
            redirect_header("category.php", 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
        }
        $params = $_POST;
        if ( $cat_handler->setCat($params) ) {
            $stop = _GROUPS_AM_SUCCEED;
        } else {
            $stop = _GROUPS_AM_FAILED;
        }
        
        redirect_header("category.php",3,$stop);
        break;
        
    default:
        exit();
}
